package com.campus.event.service;

import com.campus.event.entity.User;
import com.campus.event.repository.UserRepository;
import com.campus.event.util.SM4Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    // 新增或更新用户时加密手机号
    public User saveUser(User user, String plainPhone) {
        user.setEncryptedPhone(SM4Util.encrypt(plainPhone));
        if (user.getRole() == null) {
            user.setRole("USER");
        }
        return userRepository.save(user);
    }

    // 查询用户时解密手机号
    public User getUser(Long id) {
        User user = userRepository.findById(id).orElse(null);
        if (user != null) {
            user.setPhone(SM4Util.decrypt(user.getEncryptedPhone()));
        }
        return user;
    }
} 